/**
 * Created by admin on 2017/5/4.
 */

//文件下载
function downloadFile(fileName, content) {
    var aLink = document.createElement('a');
    var blob = new Blob([content]);
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent("click", false, false);//initEvent 不加后两个参数在FF下会报错, 感谢 Barret Lee 的反馈
    aLink.download = fileName;
    aLink.href = URL.createObjectURL(blob);
    aLink.dispatchEvent(evt);
}
//根据不同的浏览器使用相应的方式来创建异步对象aaaaaaaa
function createXmlHttp() {
    xhobj = false;
    try {
        xhobj = new ActiveXObject("Msxml2.XMLHTTP");  //iemsxml3.0+
    } catch (e) {
        try {
            xhobj = new ActiveXObject("Microsoft.XMLHTTP");  //iemsxml2.6
        } catch (e2) {
            xhobj = false;
        }
    }
    if (!xhobj && typeof XMLHttpRequest != "undefined")  //Firefox、Opera 8.0+,Safari,谷歌浏览器
    {
        xhobj = new XMLHttpRequest();
    }
    return xhobj;
}

function ExportPrint(ExportBtn,PirntBtn,DataAreaName,EnportPngName) {
    //图标打印
    $("#" + PirntBtn).on("click", function (event) {
        event.preventDefault();

        html2canvas(document.getElementById(DataAreaName), {
            allowTaint: true,
            taintTest: false,
            onrendered: function (canvas) {
                canvas.id = "printcanvas";
                //document.body.appendChild(canvas);
                //生成base64图片数据
                var dataUrl = canvas.toDataURL();
                var newImg = document.createElement("img");
                newImg.src = dataUrl;
                /* document.body.appendChild(newImg);  */
                /* window.open(newImg.src); */
                var printWindow = window.open(newImg.src);
                // printWindow.document.write();
                printWindow.document.write('<img src="' + newImg.src + '" />')
                printWindow.print();
            }
        });

    });

    // 加工image data，替换mime type 图标下砸
    $("#" + ExportBtn).off('click').on("click", function (event) {
        event.preventDefault();
        html2canvas(document.getElementById(DataAreaName), {
            allowTaint: true,
            taintTest: false,
            onrendered: function (canvas) {
                canvas.id = "mycanvas";
                //document.body.appendChild(canvas);
                //生成base64图片数据
                var type = 'png';
                var dataUrl = canvas.toDataURL(type);
                var imgData = dataUrl.replace(_fixType(type), 'image/octet-stream');
                saveFile(imgData, EnportPngName+'.png');
            }
        });

    });

    var _fixType = function (type) {
        type = type.toLowerCase().replace(/jpg/i, 'jpeg');
        var r = type.match(/png|jpeg|bmp|gif/)[0];
        return 'image/' + r;
    };
    var saveFile = function (data, filename) {
        var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
        save_link.href = data;
        save_link.download = filename;
        var event = document.createEvent('MouseEvents');
        event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        save_link.dispatchEvent(event);
    };


}